import axios from "../../config/http";
let Util = {
    "url":"",
    "method":"post",
    "headers":{
      "client":"pc",
      "Content-Type":"application/json"
    },
    data:{},
    myRequest(Props){
      this.method = Props["method"] ? Props["method"] : this.method;
      this.url = Props["url"];
      this.data = Props['data'];
      this.headers['Content-Type'] = Props['ContentType'] ? Props["ContentType"] : this.headers['Content-Type'];
      axios({
        "url":this.url,
        "method":this.method,
        "headers":this.headers,
        "data":this.data
      }).then(res => {
        Props.sCallBack && Props.sCallBack(res);
      }).catch(res => {
        Props.eCallBack && Props.eCallBack(res);
      });
    },
    isLogin(){
      let token = localStorage.getItem("token");
      if(token === null || token === "" || token === undefined){
        return false;
      }else{
        return {
          "token":token
        }
      }
    },
    dataURLtoBlob(dataurl) {
      let arr = dataurl.split(',');
      let mime = arr[0].match(/:(.*?);/)[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    },
    blobToDataURL(blob, callback) {
      let a = new FileReader();
      a.readAsDataURL(blob);
      a.onload = function (e) { callback(e.target.result); }
    },
    limitworld(worlds, wordlength, mark) {
      if (worlds == null || worlds === ""){
        return "";
      }else{
        var nowLength = worlds.length;
        var newWorlds = "";
        if (nowLength > wordlength) {
          newWorlds = worlds.substr(0, wordlength) + mark;
        } else {
          newWorlds = worlds;
        }
        return newWorlds;
      }
    },
    toObj(arr){
      let obj = {};
      for (let i = 0, j = arr.length; i < j; i++) {
        obj[arr[i]] = true;
      }
      return obj;
    },
    toArr(obj){
      let newArr = [];
      for (let attr in obj) {
        if(obj.hasOwnProperty(attr)){
          newArr.push(attr);
        }
      }
      return newArr;
    }
};

export default Util
